package com.juku2024.juku.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import com.juku2024.juku.R
import android.animation.ObjectAnimator
import android.content.Intent
import android.view.animation.AccelerateInterpolator
import android.view.animation.DecelerateInterpolator
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.juku2024.juku.WatchingActivity
import com.juku2024.juku.tools.RxUtils


class VideoWatchingView(context: Context, attributeSet: AttributeSet) : ConstraintLayout(context, attributeSet) {

    private var isShowingDes = false
    private val tvWatchManager: TextView
    init {
        val view = LayoutInflater.from(context).inflate(R.layout.view_watching, this, true)
        view.setBackgroundResource(R.drawable.shape_watching)
        tvWatchManager = view.findViewById(R.id.tvWatchManager)
        tvWatchManager.setOnClickListener {
            val intent = Intent(context, WatchingActivity::class.java)
            context.startActivity(intent)
        }
    }

    fun show() {
        isShowingDes = true
        val animator: ObjectAnimator = ObjectAnimator.ofFloat(this, "alpha", 0f, 1f)
        animator.duration = 200
        animator.interpolator = DecelerateInterpolator()
        animator.start()
        // 自动隐藏
        RxUtils.timer(5000) {
            hide()
        }
    }

    fun isShowing(): Boolean {
        return isShowingDes
    }

    fun hide() {
        if (isShowingDes) {
            isShowingDes = false
            val animator: ObjectAnimator = ObjectAnimator.ofFloat(this, "alpha", 1f, 0f)
            animator.duration = 200
            animator.interpolator = AccelerateInterpolator()
            animator.start()
        }
    }
}